import 'package:bxultimate/data/request/organization_request.dart';
import 'package:bxultimate/domain/application/organization/OrganizationAnnouncementTemplateCreateDto.dart';
import 'package:bxultimate/domain/application/organization/OrganizationAnnouncementTemplateDto.dart';
import 'package:bxultimate/domain/common/app_single_option.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import 'state.dart';

class OrganizationStatisticsTemplateCreateController extends GetxController {
  final OrganizationStatisticsTemplateCreateState state =
      OrganizationStatisticsTemplateCreateState();

  TextEditingController templateNameController = TextEditingController();
  TextEditingController placeholderController = TextEditingController();
  TextEditingController placeholderTwoController = TextEditingController();
  TextEditingController radioTextController = TextEditingController();

  // 当前是编辑还是删除，   编辑是对应的索引    新增是-1
  var currentEditIndex = -1;


  var radioList = [
    AppSingleOption(label: '年-月-日 时:分', value: "1"),
    AppSingleOption(label: '年-月-日', value: "2"),
    AppSingleOption(label: '时:分', value: "3"),
  ];

  var templateId = Get.arguments;
  
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    
    getData();
  }
  
  getData()async{
    if(templateId != null){
      OrganizationAnnouncementTemplateDto result = await OrganizationRequest.getStatisticsTaskTeamplateInfo(templateId);
      templateNameController.text = result.templateName!;
      state.formItem = result.content!;
    }
  }

  // 必填选填切换
  bool requiredChange(bool value) {
    state.requiredSelected = value;
    return value;
  }

  void addFromItem(index) {
    OrganizationAnnouncementTemplateCreateDto dto =
        OrganizationAnnouncementTemplateCreateDto();

    if (placeholderController.text == '') {
      SmartDialog.showNotify(msg: '请输入标题', notifyType: NotifyType.failure);
      return;
    }
    // 文本和数字
    if (index == 0 || index == 1) {
      dto = dto.copyWith(
          placeholder: placeholderController.text,
          placeholderTwo: placeholderTwoController.text,
          required: state.requiredSelected,
          type: index + 1);
    }
    // 日期
    if (index == 2) {
      Attributes attributes = Attributes();
      List<TimeList> timeList = [
        TimeList(time: '年-月-日 时:分', icon: 'tn-icon-success', type: 1),
        TimeList(time: '年-月-日', icon: 'tn-icon-success', type: 2),
        TimeList(time: '时:分', icon: 'tn-icon-success', type: 3),
      ];
      attributes = attributes.copyWith(
          timeindex: int.parse(state.selectDate), timeList: timeList);
      dto = dto.copyWith(
          placeholder: placeholderController.text,
          placeholderTwo: placeholderTwoController.text,
          required: state.requiredSelected,
          type: index + 1,
          attributes: attributes);
    }

    if(currentEditIndex == -1){
      state.formItem.add(dto);
    }else {
      state.formItem[currentEditIndex] = dto;
    }
    update();
    Get.back();
  }

  // 删除
  void removeFormItem(index) async {
    state.formItem.removeAt(index);
    update();
  }

  void dateChange(String? selectedId) {
    state.selectDate = selectedId!;
  }

  addRadioForm(OrganizationAnnouncementTemplateCreateDto p1) {
    if(currentEditIndex == -1){
      state.formItem.add(p1);
    }else {
      state.formItem[currentEditIndex] = p1;
    }
    update();
    Get.back();
  }

  void create() async {
    
    if(templateNameController.text == ''){
      SmartDialog.showNotify(msg: '请填写模板名称', notifyType: NotifyType.failure);
      return;
    }
    
    await OrganizationRequest.insertOrUpdateTemplate({
      "templateName": templateNameController.text,
      "content": state.formItem,
      "id":templateId
    });
    SmartDialog.showNotify(msg: '操作成功', notifyType: NotifyType.success);
    Get.back();
  }

}
